<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 会吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Shop\Api\CategoryApi;
use Think\Controller;
use Shop\Api\ProductApi;
use Admin\Api\UserPictureApi;
use Shop\Api\ProductSkuApi;
use Weixin\Api\WxuserApi;
use Shop\Api\BannersApi;
class IndexController extends HomeController {

	/*
	 * 首页 分为两种情况需要异步加载
	 *
	 * */
    public function index(){
        //调出发布的商品
        $page['curpage'] = I('curpage') ? I('curpage') : 1;//默然首页第一页
        $page['size'] = 20;
        $map['status'] = 1;
        //这里的商品一般都是一件当这一被下单时设置商品的status为0 在支付成功后再设置sku中的数量为零
        //搜索查询
        $maplike = I('name');
        if(!empty($maplike)){
            $map['name'] = array('like','%'.$maplike.'%');
            $this->assign('name',$maplike);//用于分页条件筛选
        }
        //搜索分类的所有子分类生成查询条件
        $pid = I('pid');
        if(!empty($pid)){
            $category = apiCall(CategoryApi::QUERY_NO_PAGING, array('','','id,parent'));
            $cateid = subclass_ids($category['info'],$pid);
            if(is_null($cateid)){
                $cateid = array();
            }
            array_unshift($cateid,$pid);
            $catestr = implode(',',$cateid);
            $map['cate_id'] = array('in', $catestr);
            $this->assign('pid',$pid);
        }else{
            $map['cate_id'] = array('not in', '4,23,26');
        }
        $res = apiCall(ProductApi::PAGE_ALL_PRODUCT, array($map,$page,'createtime desc','id,cate_id'));//分页数据可能会大于有的记录数所以在里面做了处理防止报错
        //下面这一块主要是取出图片的组装
        if($res['status'] && !is_null($res['info'])) {
            $productidarr = array_column($res['info'], 'id');
            $productidstr = implode(',', $productidarr);
            $pimap['id'] = array('in', $productidstr);
            $res = apiCall(ProductApi::ALLQUERY, array($pimap, array('curpage'=>0,'size'=>120), 'createtime desc', '', 'id,uid,name,createtime,updatetime,synopsis,img_id,type'));
            foreach ($res['info']['list'] as $vo) {
                $imgcondition['id'] = $vo['img_id'];
                $img = apiCall(UserPictureApi::GET_INFO, array($imgcondition));
                $skucondition['product_id'] = $vo['id'];
                $productsku = apiCall(ProductSkuApi::GET_INFO, array($skucondition));
                $productimglist[$vo['id']]['id'] = $vo['id'];
                $productimglist[$vo['id']]['name'] = $vo['name'];
                //计算发布多久了
                $lasttime = date('U') - $vo['createtime'];
                if ($lasttime < 3600 * 24) {
                    if(round($lasttime / 3600) == 0){
                        $lasttime = round($lasttime / 60) . '分钟前';
                    }else{
                        $lasttime = round($lasttime / 3600) . '小时前';
                    }
                } else {
                    $lasttime = round($lasttime / (3600 * 24)) . '天前';
                }
                $productimglist[$vo['id']]['lasttime'] = $lasttime;
                $productimglist[$vo['id']]['synopsis'] = mb_substr($vo['synopsis'],0,60, 'utf-8').'...' ;
                if (count($productimglist[$vo['id']]['imglist']) < 3) {
                    $productimglist[$vo['id']]['imglist'][] = $img['info'];//需要判断图片是不是大于3张了
                }
                $productimglist[$vo['id']]['price'] = $productsku['info']['price'];
                $productimglist[$vo['id']]['ori_price'] = $productsku['info']['ori_price'];
                $productimglist[$vo['id']]['quantity'] = $productsku['info']['quantity'];//库存
                //将发布者的信息查出放入数组
                $Wusermap['id'] = $vo['uid'];
                $productwx = apiCall(WxuserApi::GET_INFO, array($Wusermap));
                $productimglist[$vo['id']]['nickname'] = $productwx['info']['nickname'];
                $productimglist[$vo['id']]['avatar'] = $productwx['info']['avatar'];
            }

            //下面第一种情况用于AJAX异步请求
            if ($page['curpage'] > 1) {
                    $strinfo = '';
                    foreach ($productimglist as $product) {
                        $strinfo = $strinfo . "<div class='good-content'>
                                   <div class='good-top'>
                                     <div class='good-userimg'><img src=" . $product['avatar'] . " width='50px' height='50px'></div>
                                     <div class='good-name'><span class='good-sname'>" . $product['nickname'] . "</span><br/><span class='good-time'>" . $product['lasttime'] . "</span></div>
                                     <div class='good-price'><i class='fa fa-jpy'></i><span>" . $product['price'] . "</span></div>" . "
                                     </div>
                                     <a class='good-main' href=".U('index/good_detail',array('id' => $product['id'])).">
                            <div class='good-mtop'>" . $product['name'] . "</div>
                            <div class='good-intr'><p>" . $product['synopsis'] . "</div>
                            <div class='good-img'>";
                        $imgstr = '';
                        foreach ($product['imglist'] as $imglist) {
                            if(!empty($imglist['path'])){
                                $imgstr = $imgstr ."<div class='good-img-list'><img src=" . "http://muyin.8raw.com/api.php/picture/index?id=".$imglist['id'] ."&size=160"."></div>";
                            }
                        }
                        $strinfo = $strinfo . $imgstr . "</div>
                        </a>
                    </div>";
                    }
                    echo $strinfo;
            } else {
                //首页第一页输出
                //调出首页顶部的幻灯片
                $banners = $this->select_slide(1);
                $this->assign('banners',$banners);//幻灯片赋值

                $this->assign('product', $productimglist);
                $this->display();
            }
        }else{
            if($page['curpage'] > 1 ){
                echo false;//防止ajax请求的报错
            }else{
                $banners = $this->select_slide(1);
                $this->assign('banners',$banners);//幻灯片赋值
                $this->display();
            }
        }

    }


    /*
     * 幻灯片的读取
     *
     * */
    public function select_slide($position){
        $bannermap['position'] = $position;
        $bannersres = apiCall(BannersApi::ACCORD_BANNERS, array($bannermap,'','sort'));
        //首页幻灯片的判断
        if($bannersres['status']){
            $banners = array();
            foreach($bannersres['info'] as $vo){
                if(count($banners) < 3){
                        $banners[] = $vo;
                }
            }
        }else{
            $this->error('首页幻灯片读取失败 抱歉');
        }
        return $banners;

    }


    /*
     * 宝贝详情
     * */
    //这里可以和上面的一起封装一个函数
    public function good_detail(){
        $product['id'] = I('id');
        $res = apiCall(ProductApi::ALLQUERY, array($product,'','','','id,uid,name,createtime,synopsis,dt_origin_country,postage,img_id,type'));
        foreach($res['info']['list'] as $vo){
            $imgcondition['id'] = $vo['img_id'];
            $img = apiCall(UserPictureApi::GET_INFO, array($imgcondition));
            $skucondition['product_id'] = $vo['id'];
            $productsku = apiCall(ProductSkuApi::GET_INFO, array($skucondition));
            $productimglist[$vo['id']]['id'] = $vo['id'];//商品id
            $productimglist[$vo['id']]['uid'] = $vo['uid'];//发布商品的用户id
            $productimglist[$vo['id']]['name'] = $vo['name'];

            //计算发布多久了
            $lasttime = time() - $vo['createtime'];
            if ($lasttime < 3600 * 24) {
                if(round($lasttime / 3600) == 0){
                    $lasttime = round($lasttime / 60) . '分钟前';
                }else{
                    $lasttime = round($lasttime / 3600) . '小时前';
                }
            } else {
                $lasttime = round($lasttime / (3600 * 24)) . '天前';
            }
            $productimglist[$vo['id']]['lasttime'] = $lasttime;
            $productimglist[$vo['id']]['synopsis'] = $vo['synopsis'];//介绍
            $productimglist[$vo['id']]['dt_origin_country'] = $vo['dt_origin_country'];//货源
            $productimglist[$vo['id']]['postage'] = $vo['postage'];//邮费
            if(count($productimglist[$vo['id']]['imglist']) < 6){
                $productimglist[$vo['id']]['imglist'][] = $img['info'];//需要判断图片是不是大于6张了
            }
            $productimglist[$vo['id']]['price'] = $productsku['info']['price'];
            $productimglist[$vo['id']]['ori_price'] = $productsku['info']['ori_price'];
            //将发布者的信息查出放入数组
            $Wusermap['id'] = $vo['uid'];
            $productwx = apiCall(WxuserApi::GET_INFO, array($Wusermap));
            $productimglist[$vo['id']]['nickname'] = $productwx['info']['nickname'];
            $productimglist[$vo['id']]['avatar'] = $productwx['info']['avatar'];
        }
        //购买商品的用户id
        $this->assign($productimglist[$vo['id']]);
        $this->assign('buyuid',$this->userinfo['uid']);
        $this->display();
    }
}